<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/2/2
 * Time: 15:15
 */
namespace app\home\controller;
use think\Controller;
use think\Db;

class ResignController extends CommonController
{
    /**
     * register 展示注册页面
     * @return 返回HTML页面
     */
    public function register(){
        return $this->fetch('index/student_resign');
    }

    /**
     * checkPhone 检查手机号是否已被注册
     * @return 返回查询结果
     */
    public function checkPhone(){
        $phone=input("phone");
        $res = Db::table('ew_student')->column('stu_phone');
        if(in_array($phone,$res) == false){
            $resultData['status'] = 1;
            return json_encode($resultData);
        }else{
            $resultData['status'] = 0;
            $resultData['msg'] = "手机号已被注册....";
            return json_encode($resultData);
        }
    }

    /**
     * createVerify 生成验证码
     */
    public function createVerify(){
            $phone=input("get.phone");
            $num = $this->GetRandStr(6);
            // key值
            $key = "eZGkdwVntwCyjqQz";
            // 密钥
            $secret = "z0gX2W2lfs0uejZSB7ACCbSMvtRPx7dK";
            // 签名id
            $sign = "467";
            // 模板id
            $templateId = "500";
            //接受验证码手机号
            $mobile = "86".$phone;
            // 6位验证码
            $content = $num;
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, "http://api.1cloudsp.com/intl/api/v2/send?accesskey={$key}&secret={$secret}&sign={$sign}&templateId={$templateId}&mobile={$mobile}&content={$content}");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
            //释放curl句柄
            curl_close($ch);
            //打印获得的数据
            $info = json_decode($output,true);
            if($info['code'] == '0'){
                $arr = Db::table('ew_phonecode')->column('phone');
                $data = [
                    'phone'=>$phone,
                    'code'=>$num,
                    'time'=>date('Y-m-d H:i:s',time()),
                    'type'=>1
                ];
                if(in_array($phone,$arr) == false){
                    Db::table('ew_phonecode')->insert($data);
                }else{
                    Db::table('ew_phonecode')->where('phone',$phone)->update($data);
                }
                $resultData['status'] = 1;
                return json_encode($resultData);
            }else{
                $resultData['status'] = 0;
                $resultData['msg'] = "发送失败";
                return json_encode($resultData);
            }
    }

    /**
     * GetRandStr 生成随机数
     * @return $resault 返回生成的随机数
     */
    public function GetRandStr($len)
    {
        $chars = array(
            "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        );
        $charsLen = count($chars) - 1;
        shuffle($chars);
        $output = "";
        for ($i=0; $i<$len; $i++)
        {
            $output .= $chars[mt_rand(0, $charsLen)];
        }
        return $output;
    }

    public function signIn(){
//        var_dump(($_POST));die;
        if(input('') !== ""){
            extract(input(''));
            $res = Db::table('ew_student')->column('stu_phone');
            if(in_array($phone,$res) == false){
                $arr = Db::table('ew_phonecode')
                    ->where(['phone'=>$phone,'type'=>1])
                    ->field('phone,code,time')
                    ->find();
                if(empty($arr)){
                    $resultData['status'] = 0;
                    $resultData['msg'] = "请先发送验证码....";
                    return json_encode($resultData);
                }else{
                    $startTime = strtotime($arr['time']);
                    $nowTime = time();
                    if($nowTime - $startTime >= 600 ){
                        $resultData['status'] = 0;
                        $resultData['msg'] = "验证码已过期....";
                        return json_encode($resultData);
                    }else if($arr['code'] !== $verify){
                        $resultData['status'] = 0;
                        $resultData['msg'] = "验证码错误....";
                        return json_encode($resultData);
                    }else{
                        $pwd = md5($pwd);
                        $res = Db::table('ew_student')->insert(['stu_phone'=>$phone,'stu_password'=>$pwd,'stu_createtime'=>date('Y-m-d H:i:s',time())]);
                        if($res){
                            $resultData['status'] = 1;
                            $resultData['msg'] = "注册成功！";
                            return json_encode($resultData);
                        }else{
                            $resultData['status'] = 0;
                            $resultData['msg'] = "注册失败！";
                            return json_encode($resultData);
                        }
                    }
                }
            }else{
                $resultData['status'] = 0;
                $resultData['msg'] = "手机号已被注册....";
                return json_encode($resultData);
            }
        }
    }

}